/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package skat;

import rlpw.lang.Log;
import java.util.*;

import rlpw.util.*;
import skat.collections.*;
import skat.comparators.Comparator;
import skat.decks.Deck;
import skat.games.Game;
import skat.player.*;

/**
 * 
 * @author weinpau
 */
public class Main {
	// FIXME: Felder an geeignete Stelle schieben und evtl. kapseln

	public static Comparator comp;
	public static Deck deck;
	public static Desk desk;
	public static List<String> cmdln;
	public static PlayerGroupList groups;

	Main() throws Exception {
		// auf der windowskonsole mit -cp:Cp858 starten für korrekte Umlaute ;)
		for (String s : cmdln) {
			if (s.startsWith("-cp:")) {
				Log.setCodepage(s.substring(4));
			}
		}
		String game = Games.ACORN.toString().toLowerCase();
		comp = Comparator.make(game);
		deck = Deck.make();
		desk = new Desk(Game.make(game));
		groups = new PlayerGroupList();

		CardStack stack = new CardStack(deck);

		for (int i = 0; i < 20; i++) {
			stack.reset();
			stack.shuffle();
			Hand h = stack.getHand();
			GameFinder gf = new GameFinder(h);
			Log.outln(">> Analyse Hand ", i + 1, ":\n",h,
					"\n>> Raten: ", 
					"\n - Grand  = ", gf.grandRate(), 
					"\n - Eichel = ", gf.acornRate(), 
					"\n - Grün   = ", gf.greenRate(), 
					"\n - Rot    = ", gf.redRate(), 
					"\n - Schell = ", gf.schellRate(), 
					"\n - Null   = ", gf.nullRate());
		}
		stack.reset();
		stack.shuffle();
		
		System.exit(0); // Notbremse ;)

		RotationSet<Player> players = new RotationSet<Player>(3);
		Random rnd = new Random();

		for (int i = 0; i < 3; i++) {

			Hand h = stack.getHand();
			Player p = new Player();
			p.setHand(h);
			p.setName("Spieler " + (i + 1));
			p.getMemory().setDefaultForgettingRate(Math.random() * .3);

			Log.outln(h, " = ", h.getPoints(), " Punkte");

			players.add(p);
		}

		Hand talon = stack.getTalon();

		Log.outln(talon.toString(true));

		Log.outln("\nSpielsimulation:");

		int who = rnd.nextInt(players.getCount());

		PlayerGroup pgSingle = new PlayerGroup(players.get(who));
		PlayerGroup pgTeam = new PlayerGroup(players.next(), players.next());

		groups.add(pgSingle);
		groups.add(pgTeam);

		players.get(who).getMemory()
				.add(new Pair<Player, Card>(players.get(who), talon.get(0)), 0);
		players.get(who).getMemory()
				.add(new Pair<Player, Card>(players.get(who), talon.get(1)), 0);

		pgSingle.addCards(talon);

		for (int i = 0; i < 10; i++) {
			/*
			 * Log.outln("########################################");
			 * 
			 * for (int j = 0; j < 3; j++) { Player p = players.next(); for (int
			 * k = 0; k < 3; k++) { Player p2 = players.next(); Log.outln("\n",
			 * p, " denkt, dass ", p2, " folgende Karten hat:");
			 * 
			 * Log.outln(p.getLogicalCards(p2));
			 * 
			 * }
			 * 
			 * }
			 */

			Log.outln("\n", ">> ", players.get(who).getName(), " kommt raus:");
			desk.clear();

			for (int j = 0; j < 3; j++) {
				if (j == 0) {
					players.get(who).placeCard();
				} else {
					players.next().placeCard();
				}
				if (j > 0) {
					Log.out(", ");
				}
				Log.out(desk.get(j).right().toString(true));
			}

			who = players.indexOf(desk.getHighestPair().left());

			Player winner = desk.getHighestPair().left();
			if (pgSingle.contains(winner)) {
				pgSingle.addCards(desk.getAllCards());
			} else {
				pgTeam.addCards(desk.getAllCards());
			}

			Log.outln("\n>> Stich geht an: ", winner.getName());

			/*
			 * Log.outln("\n Spielergedächtnis:");
			 * 
			 * for (int j = 0; j < 3; j++) { Player p = players.next();
			 * Log.outln(p, ":"); Log.outln(p.getMemory()); }
			 */

		}

		Log.outln(">> Spielende:\n ");
		Log.outln(pgSingle.get(0), ": ", pgSingle.getPoints(), " Punkte");
		Log.outln("     Team: ", pgTeam.getPoints(), " Punkte");

	}

	public static void main(String[] args) {
		try {
			cmdln = new ArrayList<String>();
			for (String s : args) {
				cmdln.add(s);
			}
			new Main();
		} catch (Exception ex) {
			ex.printStackTrace();
			System.exit(1);
		}
	}
}
